package com.linjacy.container.product.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


import com.linjacy.container.product.annotation.MyLog;
import com.linjacy.container.product.service.impl.OrderServiceImpl;
import com.linjacy.container.product.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.linjacy.container.product.entity.OrderEntity;
import com.linjacy.container.product.service.OrderService;
import com.linjacy.common.utils.PageUtils;
import com.linjacy.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author linjacy
 * @email 939886352@gmail.com
 * @date 2021-01-03 16:04:54
 */
@RestController
@RequestMapping("product/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    OrderServiceImpl orderServiceImpl;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 列表
     */
    @MyLog(value = "5",logLevel = "debugList")
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);
        return R.ok().put("page", page);
    }

    @MyLog(value = "5",logLevel = "debugGetOrderPage")
    @RequestMapping("/list/getOrderPage")
    public R getOrderPage(@RequestParam Map<String, Object> params){
        PageUtils data=orderService.getOrderPage(params);
        return R.ok().put("page",data);
    }

    /**
     * 获取实时滚动订单
     * @param
     * @return
     */
    @MyLog(value = "5",logLevel = "infoGetNowOrder")
    @RequestMapping("/list/getNowOrder")
    public R getOrder(){
        List<Map<String,String>> data=orderService.getNowOrder();
        return R.ok().put("data",data);
    }

    /**
     * 完成订单
     * @param cid
     * @return
     */
    @MyLog
    @RequestMapping("/list/changeStatus/{cid}")
    public R changeStatus(@PathVariable("cid") Long cid){
        orderService.changeOrderStatus(cid);
        return R.ok();
    }

    /**
     * 信息
     */
    @MyLog
    @RequestMapping("/info/{cid}")
    public R info(@PathVariable("cid") Long cid){
		OrderEntity order = orderService.getById(cid);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }
    @RequestMapping("/saveOrder")
    public R saveOrder(@RequestBody OrderEntity order){
        orderService.saveOrder(order);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }
    /**
     * 修改订单
     */
    @RequestMapping("/updateOrder")
    public R update(@RequestBody OrderVo orderVo){
        orderService.updateByOrderVo(orderVo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] cids){
		orderService.removeByIds(Arrays.asList(cids));

        return R.ok();
    }
    @RequestMapping("/delete/{id}")
    public R deleteById(@PathVariable("id") Long cid){
        orderService.deleteById(cid);
        return R.ok();
    }
    /**
     * 发配订单
     */
    @PostMapping("/setTransporter")
    public R setTransporter(@RequestParam Map<String, Object> params){
        orderService.changeTransporterAndOrder(params);
        return R.ok();
    }
    /**
     * 导出报表
     */
    @PostMapping("/export")
    public R exportExcel(@RequestBody Map<String, Object> params,HttpServletResponse response, HttpServletRequest request) throws Exception {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String dateStr = formatter.format(date);
        String fileName = "集装箱订单报表";
        fileName += dateStr;
        orderService.export(params,fileName,response);
        return R.ok();
    }
    /**
     * 生成数据分析图
     */
    @PostMapping("/createAnalysis")
    public R createAnalysis(@RequestBody Map<String, Object> params){
        Map<String,Object> map=orderService.createAnalysis(params);
        return R.ok().put("data",map);
    }
    /**
     * 生成地图分析图
     */
    @PostMapping("/createMap")
    public R createMap(){
        List<Map<String,Integer>> list=orderService.getMapList();
        return R.ok().put("data",list);
    }
    /**
     * 生成饼图图
     */
    @PostMapping("/createBiscuit")
    public R createBiscuit(){
        List<Map<String,Integer>> list=orderService.getBiscuit();
        return R.ok().put("data",list);
    }
}
